﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using LynxBlogAPI.Models;

namespace LynxBlogAPI.Controllers
{
    public class CommentPolicyController : ApiController
    {
        private LynxWebLog_Context db = new LynxWebLog_Context();

        // GET api/CommentPolicy
        public IQueryable<CommentPolicy> GetCommentPolicies()
        {
            return db.CommentPolicies;
        }

        // GET api/CommentPolicy/5
        [ResponseType(typeof(CommentPolicy))]
        public IHttpActionResult GetCommentPolicy(string id)
        {
            CommentPolicy commentpolicy = db.CommentPolicies.Find(id);
            if (commentpolicy == null)
            {
                return NotFound();
            }

            return Ok(commentpolicy);
        }

        // PUT api/CommentPolicy/5
        public IHttpActionResult PutCommentPolicy(string id, CommentPolicy commentpolicy)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != commentpolicy.blogid)
            {
                return BadRequest();
            }

            db.Entry(commentpolicy).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CommentPolicyExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST api/CommentPolicy
        [ResponseType(typeof(CommentPolicy))]
        public IHttpActionResult PostCommentPolicy(CommentPolicy commentpolicy)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.CommentPolicies.Add(commentpolicy);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (CommentPolicyExists(commentpolicy.blogid))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = commentpolicy.blogid }, commentpolicy);
        }

        // DELETE api/CommentPolicy/5
        [ResponseType(typeof(CommentPolicy))]
        public IHttpActionResult DeleteCommentPolicy(string id)
        {
            CommentPolicy commentpolicy = db.CommentPolicies.Find(id);
            if (commentpolicy == null)
            {
                return NotFound();
            }

            db.CommentPolicies.Remove(commentpolicy);
            db.SaveChanges();

            return Ok(commentpolicy);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool CommentPolicyExists(string id)
        {
            return db.CommentPolicies.Count(e => e.blogid == id) > 0;
        }
    }
}